package cn.wolfcode.luowowo.mgrsite.job;

import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.cache.service.IStrategyStatsCacheService;
import cn.wolfcode.luowowo.cache.vo.StrategyStats;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

//单纯交给spring管理还不够,还要在启动类上面贴上注解@EnableScheduling
//@Component
public class HotDataPersistenceJob {
    @Reference
    private IStrategyStatsCacheService strategyStatsCacheService;
    @Reference
    private IStrategyDetailService detailService;

    //贴上下面这个注解,就是定义了一个定时器,会按照cron表达式限定的时间(1分钟一次)循环执行里面的方法体
    @Scheduled(cron = "0 1/1 * * * ? ")
    public void hotDataPersistence(){
        //从Redis获取所有StrategyStats
        List<StrategyStats> list = strategyStatsCacheService.listStats();
        for (StrategyStats stats : list) {
            //存进mysql,注意是存到detail表里面
            detailService.updateByStats(stats);
        }
    }

}
